
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>对 observables 作出响应 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="change-status.html" />
    
    
    <link rel="prev" href="observable-decorators.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="get-started.html">
            
                <a href="get-started.html">
            
                    
                    mobx搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    mobx入门
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="observable-data.html">
            
                <a href="observable-data.html">
            
                    
                    observable可观察的状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="observable-decorators.html">
            
                <a href="observable-decorators.html">
            
                    
                    observable装饰器
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.3" data-path="reactions.html">
            
                <a href="reactions.html">
            
                    
                    对 observables 作出响应
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="change-status.html">
            
                <a href="change-status.html">
            
                    
                    改变 observables状态
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../usage/">
            
                <a href="../usage/">
            
                    
                    应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../usage/with-react.html">
            
                <a href="../usage/with-react.html">
            
                    
                    在react中使用mobx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../usage/usecase.html">
            
                <a href="../usage/usecase.html">
            
                    
                    react-app案例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../thanks/README.md">
            
                <span>
            
                    
                    感谢
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >对 observables 作出响应</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="mobx&#x5165;&#x95E8;">mobx&#x5165;&#x95E8;</h1>
<h2 id="&#x5BF9;-observables-&#x4F5C;&#x51FA;&#x54CD;&#x5E94;">&#x5BF9; observables &#x4F5C;&#x51FA;&#x54CD;&#x5E94;</h2>
<h3 id="0-&#x57FA;&#x7840;&#x4EE3;&#x7801;&#xFF1A;">0. &#x57FA;&#x7840;&#x4EE3;&#x7801;&#xFF1A;</h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {observable} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mobx&apos;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span></span>{
  @observable arr = [];
  @observable obj = {a: <span class="hljs-number">1</span>};
  @observable map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();
  @observable str = <span class="hljs-string">&apos;hello&apos;</span>;
  @observable num = <span class="hljs-number">123</span>;
  @observable bool = <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Store();
</code></pre>
<h3 id="1-computed">1. computed</h3>
<p>&#x8BA1;&#x7B97;&#x503C;&#x662F;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x73B0;&#x6709;&#x7684;&#x72B6;&#x6001;&#x6216;&#x5176;&#x5B83;&#x8BA1;&#x7B97;&#x503C;&#x884D;&#x751F;&#x51FA;&#x7684;&#x503C;,  &#x8DDF;vue&#x4E2D;&#x7684;computed&#x975E;&#x5E38;&#x76F8;&#x4F3C;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> result = computed(()=&gt;store.str + store.num);
<span class="hljs-built_in">console</span>.log(result.get());
<span class="hljs-comment">// &#x76D1;&#x542C;&#x6570;&#x636E;&#x7684;&#x53D8;&#x5316;</span>
result.observe((change)=&gt;{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;result:&apos;</span>, change);
})
<span class="hljs-comment">//&#x4E24;&#x6B21;&#x5BF9;store&#x5C5E;&#x6027;&#x7684;&#x4FEE;&#x6539;&#x90FD;&#x4F1A;&#x5F15;&#x8D77;result&#x7684;&#x53D8;&#x5316;</span>
store.str = <span class="hljs-string">&apos;world&apos;</span>;
store.num = <span class="hljs-number">220</span>;
</code></pre>
<p>computed&#x53EF;&#x4F5C;&#x4E3A;&#x88C5;&#x9970;&#x5668;&#xFF0C; &#x5C06;result&#x7684;&#x8BA1;&#x7B97;&#x6DFB;&#x52A0;&#x5230;&#x7C7B;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span></span>{
  @observable arr = [];
  @observable obj = {a: <span class="hljs-number">1</span>};
  @observable map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();

  @observable str = <span class="hljs-string">&apos;hello&apos;</span>;
  @observable num = <span class="hljs-number">123</span>;
  @observable bool = <span class="hljs-literal">false</span>;

  @computed get result(){
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.str + <span class="hljs-keyword">this</span>.num;
  }  
}
</code></pre>
<h3 id="2-autorun">2. autorun</h3>
<p>&#x5F53;&#x4F60;&#x60F3;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x5F0F;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x8BE5;&#x51FD;&#x6570;&#x672C;&#x8EAB;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x6709;&#x89C2;&#x5BDF;&#x8005;&#x65F6;,&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>mobx.autorun</code></p>
<p>&#x6240;&#x63D0;&#x4F9B;&#x7684;&#x51FD;&#x6570;&#x603B;&#x662F;&#x7ACB;&#x5373;&#x88AB;&#x89E6;&#x53D1;&#x4E00;&#x6B21;&#xFF0C;&#x7136;&#x540E;&#x6BCF;&#x6B21;&#x5B83;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x6539;&#x53D8;&#x65F6;&#x4F1A;&#x518D;&#x6B21;&#x88AB;&#x89E6;&#x53D1;&#x3002;</p>
<p>&#x7ECF;&#x9A8C;&#x6CD5;&#x5219;&#xFF1A;&#x5982;&#x679C;&#x4F60;&#x6709;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5E94;&#x8BE5;&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#xFF0C;&#x4F46;&#x4E0D;&#x4F1A;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x503C;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528;<code>autorun</code>&#x3002; &#x5176;&#x4F59;&#x60C5;&#x51B5;&#x90FD;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; <code>computed</code>&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">//aotu&#x4F1A;&#x7ACB;&#x5373;&#x89E6;&#x53D1;&#x4E00;&#x6B21;</span>
autorun(()=&gt;{
  <span class="hljs-built_in">console</span>.log(store.str + store.num);
})

autorun(()=&gt;{
  <span class="hljs-built_in">console</span>.log(store.result);
})
<span class="hljs-comment">//&#x4E24;&#x6B21;&#x4FEE;&#x6539;&#x90FD;&#x4F1A;&#x5F15;&#x8D77;autorun&#x6267;&#x884C;</span>
store.num = <span class="hljs-number">220</span>;
store.str = <span class="hljs-string">&apos;world&apos;</span>;
</code></pre>
<h3 id="3-when">3. when</h3>
<pre><code>when(predicate: () =&gt; boolean, effect?: () =&gt; void, options?)
</code></pre><p><code>when</code> &#x89C2;&#x5BDF;&#x5E76;&#x8FD0;&#x884C;&#x7ED9;&#x5B9A;&#x7684; <code>predicate</code>&#xFF0C;&#x76F4;&#x5230;&#x8FD4;&#x56DE;true&#x3002; &#x4E00;&#x65E6;&#x8FD4;&#x56DE; true&#xFF0C;&#x7ED9;&#x5B9A;&#x7684; <code>effect</code> &#x5C31;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x7136;&#x540E; autorunner(&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;) &#x4F1A;&#x88AB;&#x6E05;&#x7406;&#x3002; &#x8BE5;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6E05;&#x7406;&#x5668;&#x4EE5;&#x63D0;&#x524D;&#x53D6;&#x6D88;&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x4EE5;&#x54CD;&#x5E94;&#x5F0F;&#x65B9;&#x5F0F;&#x6765;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x6216;&#x8005;&#x53D6;&#x6D88;&#xFF0C;&#x6B64;&#x51FD;&#x6570;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;</p>
<pre><code class="lang-js">when(()=&gt;store.bool, ()=&gt;{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;when function run.....&apos;</span>);
})
store.bool = <span class="hljs-literal">true</span>;
</code></pre>
<h3 id="4-reaction">4. reaction</h3>
<p>&#x7528;&#x6CD5;: <code>reaction(() =&gt; data, (data, reaction) =&gt; { sideEffect }, options?)</code>&#x3002;</p>
<p><code>autorun</code> &#x7684;&#x53D8;&#x79CD;&#xFF0C;&#x5BF9;&#x4E8E;&#x5982;&#x4F55;&#x8FFD;&#x8E2A; observable &#x8D4B;&#x4E88;&#x4E86;&#x66F4;&#x7EC6;&#x7C92;&#x5EA6;&#x7684;&#x63A7;&#x5236;&#x3002; &#x5B83;&#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x53C2;&#x6570;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;(<em>&#x6570;&#x636E;</em> &#x51FD;&#x6570;)&#x662F;&#x7528;&#x6765;&#x8FFD;&#x8E2A;&#x5E76;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E8C;&#x4E2A;&#x51FD;&#x6570;(<em>&#x6548;&#x679C;</em> &#x51FD;&#x6570;)&#x7684;&#x8F93;&#x5165;&#x3002; &#x4E0D;&#x540C;&#x4E8E; <code>autorun</code> &#x7684;&#x662F;&#x5F53;&#x521B;&#x5EFA;&#x65F6;<em>&#x6548;&#x679C;</em> &#x51FD;&#x6570;&#x4E0D;&#x4F1A;&#x76F4;&#x63A5;&#x8FD0;&#x884C;&#xFF0C;&#x53EA;&#x6709;&#x5728;&#x6570;&#x636E;&#x8868;&#x8FBE;&#x5F0F;&#x9996;&#x6B21;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x503C;&#x540E;&#x624D;&#x4F1A;&#x8FD0;&#x884C;&#x3002; &#x5728;&#x6267;&#x884C; <em>&#x6548;&#x679C;</em> &#x51FD;&#x6570;&#x65F6;&#x8BBF;&#x95EE;&#x7684;&#x4EFB;&#x4F55; observable &#x90FD;&#x4E0D;&#x4F1A;&#x88AB;&#x8FFD;&#x8E2A;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// reaction</span>
reaction(()=&gt;[store.str, store.num], (arr)=&gt;{
  <span class="hljs-built_in">console</span>.log(arr.join(<span class="hljs-string">&apos;/&apos;</span>));
})
<span class="hljs-comment">//&#x53EA;&#x8981;[store.str, store.num]&#x4E2D;&#x4EFB;&#x610F;&#x4E00;&#x503C;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;reaction&#x7B2C;&#x4E8C;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x4F1A;&#x6267;&#x884C;</span>
store.num = <span class="hljs-number">220</span>;
store.str = <span class="hljs-string">&apos;world&apos;</span>;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="observable-decorators.html" class="navigation navigation-prev " aria-label="Previous page: observable装饰器">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="change-status.html" class="navigation navigation-next " aria-label="Next page: 改变 observables状态">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"对 observables 作出响应","level":"1.3.3","depth":2,"next":{"title":"改变 observables状态","level":"1.3.4","depth":2,"path":"basics/change-status.md","ref":"basics/change-status.md","articles":[]},"previous":{"title":"observable装饰器","level":"1.3.2","depth":2,"path":"basics/observable-decorators.md","ref":"basics/observable-decorators.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"basics/reactions.md","mtime":"2020-02-12T09:36:34.427Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-02-12T15:17:20.849Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

